<?php
require 'vendor/autoload.php';

class Searcher {

	private $rssParser;
	private $sorter;
	public $feedsXml;
	private $preferencesManager;
	private $newsManager;
	private $configManager;

	function __construct($rssParser,$sorter,$preferencesManager,$newsManager,$configurationManager){
		$this->rssParser = $rssParser;
		$this->sorter = $sorter;
		$this->preferencesManager = $preferencesManager;
		$this->newsManager = $newsManager;
		$this->configManager = $configurationManager;
	}

	function setFeedsXml($feedsXml){
		$this->feedsXml = $feedsXml;
	}

	function search($keyword, $metodo){
		$newsArrayKey =  array();
		$sourcesShortnameArray = array();
		$i = 0;
		$keywordArray = explode(" ",$keyword);
		$keywordsStringForDBSearch = $this->prepareKeyWordsForDBSearch($keywordArray);
		$sourceArray = simplexml_load_file($this->feedsXml);
		$dateLimit  = DateManager::getSearchLimit();
		foreach($sourceArray as $source) {
			if($this->preferencesManager->isSourceEnabled($source)){
				array_push($sourcesShortnameArray,(string)$source->shortName);
			}
		}
		$newsArrray = $this->newsManager->searchNewsByKeyWordFromEnabledSources($keywordsStringForDBSearch,$sourcesShortnameArray,$dateLimit);
		$cant = count($newsArrray);
		foreach($newsArrray as $news){
			$title = $news->getTitle();
			$resume = $news->getResume();
			$pubDate = $news->getPubDate();
			$weight = $this->sorter->calculateWeight($keywordArray,$title,$resume,$pubDate);
			if ($weight > 0){
				$news->setWeight($weight);
				$newsArrayKey[$i] = $news;
				$i = $i + 1;
			}
		}
		$sortedNewsArrayKey = $this->sorter->NewsSort($newsArrayKey,$metodo);
		$cant2 = count($newsArrayKey);
		return ($sortedNewsArrayKey);
	}

	 
	function searchOnDBForTrendingNews($keywords, $metodo){
		$i = 0;
		$newsArrayKey =  array();
		$keywordArray = explode(" ",$keywords);
		$keywordsStringForDBSearch = $this->prepareKeyWordsForDBSearch($keywordArray);
		$newsArrray = $this->newsManager->searchNewsByKeyWordFromAllSources($keywordsStringForDBSearch);
		foreach($newsArrray as $news){
					$title = $news->getTitle();
					$resume = $news->getResume();
					$pubDate = $news->getPubDate();
					$weight = $this->sorter->calculateWeight($keywordArray,$title,$resume,$pubDate);
					if ($weight > 0){
						$news->setWeight($weight);
						$newsArrayKey[$i] = $news;
						$i = $i + 1;
					}
				}
		$sortedNewsArrayKey = $this->sorter->NewsSort($newsArrayKey,$metodo);
		return ($sortedNewsArrayKey);
	}
	
	private function prepareKeyWordsForDBSearch($keywordArray){
		$keywordsStringForDBSearch = '';
		foreach ($keywordArray as $kw){
			$key = str_replace($this->configManager->getArrayCharacters(), "", $kw);
			$keywordsStringForDBSearch = $keywordsStringForDBSearch . '%'.$key.'%' . ' ';
		}
		return ($keywordsStringForDBSearch);
	}

	function removeWhiteSpacesExcess($filteredText){
		$filteredText = preg_replace('/\s\s+/', ' ', $filteredText);
		return $filteredText;
	}

}